package com.um.pasantiaum.cliente;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.ParseUser;
import com.um.pasantiaum.R;
import com.um.pasantiaum.objetos.Producto;
import com.um.pasantiaum.objetos.ProductoManager;

public class AgregarProductosFragment extends ListFragment {
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setHasOptionsMenu(true);

		setRetainInstance(true);
		AdminProductAdapter adapter = new AdminProductAdapter(ProductoManager
				.getProductoManager(ParseUser.getCurrentUser()).getProductos());
		setListAdapter(adapter);

	}
	private class AdminProductAdapter extends ArrayAdapter<Producto> {

		public AdminProductAdapter(ArrayList<Producto> prod) {
			super(getActivity(), 0, prod);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						com.um.pasantiaum.R.layout.list_item_product, null);
			}

			Producto p = getItem(position);
			TextView nameView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prodruct_name);
			nameView.setText(p.getNombre());
			TextView descriptionView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prodruct_description);
			descriptionView.setText(p.getDescripcion());
			TextView priceView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.product_price);
			priceView.setText(p.getPrice().toString() + " $");

			return convertView;
		}
	}
	public View onCreateView(LayoutInflater inflater, ViewGroup parent,
			Bundle savedInstanceState) {
		View v = super.onCreateView(inflater, parent, savedInstanceState);
		ListView listView = (ListView) v.findViewById(android.R.id.list);

		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
			registerForContextMenu(listView);
		else {
			listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
			listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

				@Override
				public boolean onActionItemClicked(ActionMode mode,
						MenuItem item) {
					switch (item.getItemId()) {
					case com.um.pasantiaum.R.id.menu_item_delete_producto:
						AdminProductAdapter adapter = (AdminProductAdapter) getListAdapter();
						ProductoManager manager = ProductoManager
								.getProductoManager(ParseUser.getCurrentUser());
						for (int i = adapter.getCount() - 1; i >= 0; i--) {
							if (getListView().isItemChecked(i)) {
								manager.removeProducto(adapter.getItem(i));
							}
						}
						mode.finish();
						adapter.notifyDataSetChanged();
						return true;
					default:
						return false;
					}
				}

				@Override
				public boolean onCreateActionMode(ActionMode mode, Menu menu) {
					MenuInflater inflater = mode.getMenuInflater();
					inflater.inflate(
							com.um.pasantiaum.R.menu.producto_lista_contexto,
							menu);
					return true;
				}

				@Override
				public void onDestroyActionMode(ActionMode arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public boolean onPrepareActionMode(ActionMode mode, Menu item) {
					return false;
				}

				@Override
				public void onItemCheckedStateChanged(ActionMode arg0,
						int arg1, long arg2, boolean arg3) {
					// TODO Auto-generated method stub

				}

			});
		}
		return v;
	}

}
